<template>
    <ZkModalVue :title="title" :size="'sm'" :visible="visible" @cancel="handleCancel" @ok="handleOk">
        <a-form-model ref="form" :model="info" v-bind="formLayout" :rules="rules">
            <a-form-model-item label="原密码" prop="oldPass" v-if="type==PasswordEditTypeEnum.USER_PASSWORD">
                <a-input-password placeholder="请输入原密码" type='password' v-model="info.oldPass" />
            </a-form-model-item>
            <a-form-model-item label="新密码" prop="newPass">
                <a-input-password placeholder="请输入新密码" type='password' v-model="info.newPass" />
            </a-form-model-item>
            <a-form-model-item label="确认密码" prop="sureNewPasss">
                <a-input-password placeholder="请输入确认密码" type='password' v-model="info.sureNewPasss" />
            </a-form-model-item>
        </a-form-model>
    </ZkModalVue>
</template>
<script>
    import ZkModalVue from '@/appBase/ZkModal.vue';
    // import { updatePassword } from '@/api/common/user';
    import { updatePassword } from '@/api/base';
    import { logout } from '@/modules/common/service/base';
    import { PasswordEditTypeEnum } from '../enum/enum';
    export default {
        name: "PasswordEdit",
        components: {
            ZkModalVue
        },
        data() {
            const passwordValidator = (rule, value, callback) => {
                if (value == '') {
                    callback(new Error('请输入密码'))
                } else if (value && value.length >= 6 && value.length <= 20) {
                    callback();
                } else {
                    callback(new Error('请输入6-20位字母、数字'));
                }
            };
            const sureNewPasss = (rule, value, callback) => {
                if (value != this.info.newPass) {
                    callback(new Error('两次新密码不匹配'));
                } else {
                    callback();
                }
            }
            return {
                title: '密码修改',
                PasswordEditTypeEnum,
                id: null,
                /** 类型 */
                type: 0,
                visible: false,
                /** 布局配置 */
                formLayout: {
                    layout: 'horizontal',
                    labelCol: {
                        xs: { span: 24 },
                        sm: { span: 7 }
                    },
                    wrapperCol: {
                        xs: { span: 24 },
                        sm: { span: 14 }
                    }
                },
                info: {
                    oldPass: '',
                    newPass: '',
                    sureNewPasss: ''
                },
                rules: {
                    oldPass: [{ required: true, trigger: 'blur', validator: passwordValidator }],
                    newPass: [{ required: true, trigger: 'blur', validator: passwordValidator }],
                    sureNewPasss: [
                        { required: true, trigger: 'blur', validator: passwordValidator },
                        { trigger: 'blur', validator: sureNewPasss }
                    ]
                }
            }
        },
        methods: {
            /**
             * @param { Object } param
             * @param { String } param.title 标题
             * @param { Number } param.type 类型枚举 PasswordEditTypeEnum
             * @param { Function } param.onOk
             * @param { Function } param.onCancel
             */
            show(param = {}) {
                const { id, type, title, onOk, onCancel } = param;
                this._onOk = onOk;
                this._onCancel = onCancel;
                this.title = title || '修改密码';
                this.visible = true;
                this.type = type || PasswordEditTypeEnum.USER_PASSWORD;
                this.id = id;
                this.$nextTick(() => {
                    this.$refs.form.resetFields();
                });
            },
            handleOk() {
                this.$refs.form.validate((valid) => {
                    try {
                        if (!valid) return;
                        const { id, type } = this;
                        const { oldPass, newPass, sureNewPasss } = this.info;
                        if (type == PasswordEditTypeEnum.USER_PASSWORD) {
                            // 用户密码修改
                            updatePassword({
                                oldPassword: this.info.oldPass,
                                newPassword: this.info.newPass,
                                confirmPassword: this.info.sureNewPasss
                            }).then(res => {
                                this.$message.success('修改成功');
                                logout(this).then((result) => { });
                            });
                            return;
                        }
                        if (!id) {
                            console.error('请传入对应 Id');
                            return;
                        }
                    } catch (error) {
                        console.error(error);
                    }
                });
            },
            handleCancel() {
                this.visible = false;
                if (this._onCancel) this._onCancel();
            }
        }
    }
</script>